package com.lepu.app.main.fragment;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.app.utils.UmengEvent;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragment;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.bbs.fragment.FragmentBbs;
import com.lepu.pasm.R;

public class MainTabFragmentBbs extends BaseFragment {

	private static final int MSG_TEST = 1;

	private View mMainView = null;
	private boolean mInitDataSuccess = false;// 是否加载完数据
	 public boolean mRefreshFlag = true;

	private ViewPager mViewPager = null;
	private LinearLayout mTabLayoutOne = null;
	private LinearLayout mTabLayoutTwo = null;
	private LinearLayout mTabLayoutThree = null;

	private int mCurrentTab = 0;

	public ArrayList<Fragment> fragmentList;
	public FragmentBbs bbs1; //热帖(需要刷新轮播图)
	
	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(
				R.layout.main_tab_fragment_bbs, null, false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onResume() {
		super.onResume();

		UmengHelper.OnPageStart(UmengEvent.page_luntan);
	}

	@Override
	public void onPause() {
		super.onPause();

		UmengHelper.OnPageEnd(UmengEvent.page_luntan);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInitDataSuccess = false;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess || mRefreshFlag) {
	            // 第一次进入页面获取数据
	            mRefreshFlag = false;

				mInitDataSuccess = true;
				if(bbs1 != null){
					bbs1.requestFocus(false);
				}else{
					init();
				}
				
			}
		}
	}

	/*
	 * 初始化ui控件，不要初始化数据！！！！
	 * 
	 * 初始化控件，需要用mMainView.findViewById 不能使用findViewById，否则程序会闪退的
	 */
	private void init() {
		fragmentList = new ArrayList<Fragment>();
		bbs1 = new FragmentBbs();
		FragmentBbs bbs2 = new FragmentBbs();
		FragmentBbs bbs3 = new FragmentBbs();

		Bundle data1 = new Bundle();
		data1.putInt("Type", 1);
		data1.putSerializable("MotherCircle", null);
		bbs1.setArguments(data1);

		Bundle data2 = new Bundle();
		data2.putInt("Type", 2);
		data2.putSerializable("MotherCircle", null);
		bbs2.setArguments(data2);

		Bundle data3 = new Bundle();
		data3.putInt("Type", 3);
		data3.putSerializable("MotherCircle", null);
		bbs3.setArguments(data3);

		fragmentList.add(bbs1);
		fragmentList.add(bbs2);
		fragmentList.add(bbs3);

		mViewPager = (ViewPager) mMainView.findViewById(R.id.motherCircleListViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getActivity()
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		mViewPager.setOffscreenPageLimit(2);

		ImageView newsImage = (ImageView)mMainView. findViewById(R.id.bbs_news_image);
		ImageView searchImage = (ImageView) mMainView.findViewById(R.id.bbs_search_image);
		mTabLayoutOne = (LinearLayout) mMainView.findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (LinearLayout)mMainView. findViewById(R.id.tabLayout2);
		mTabLayoutThree = (LinearLayout)mMainView. findViewById(R.id.tabLayout3);

		newsImage.setOnClickListener(mViewClickListener);
		searchImage.setOnClickListener(mViewClickListener);
		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);
		mTabLayoutThree.setOnClickListener(mViewClickListener);

		updateTabLine(mCurrentTab);

	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				mViewPager.setCurrentItem(0, false);
				updateTabLine(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				mViewPager.setCurrentItem(1, false);
				updateTabLine(1);
				break;
			case R.id.tabLayout3:

				if (mCurrentTab == 2)
					return;

				mViewPager.setCurrentItem(2, false);
				updateTabLine(2);
				break;
			case R.id.bbs_news_image:
				((BaseFragmentActivity) getActivity()).startActivity(
						new Intent(getActivity(), null), true);
				break;
			case R.id.bbs_search_image:
				// ((BaseFragmentActivity)getActivity()).startActivity(new
				// Intent(getActivity(),BbsSearchActivity.class),true);
				break;
			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			mTabLayoutThree.setSelected(false);
		} else if (currentIndex == 1) {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			mTabLayoutThree.setSelected(false);
		} else if (currentIndex == 2) {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(false);
			mTabLayoutThree.setSelected(true);
		}
	}

	public void refreshBanner(){
		if(fragmentList != null && fragmentList.size() > 0 && bbs1 != null){
			bbs1.requestFocus(false);
		}
	}
	
	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index, false);
			updateTabLine(index);
		}
	}

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_TEST:

				break;

			default:
				break;
			}
		};
	};

}
